<?php
/**
 * Created by PhpStorm.
 * User: 程序猴
 * Date: 2018/5/9
 *  Q Q: 505413
 * Time: 10:37
 */

namespace app\admin\controller;
use app\admin\controller\Base;
use think\Data;
use think\Db;
class Category extends Base
{
    public function index()
    {
        $list = Db::name('category')->select();
        $list = Data::tree($list,'name','id','pid');
        $this->assign('list',$list);
        return $this->fetch();
    }
    //修改分类列表的开启状态
    public function status_edit()
    {
        $id = input('param.id');
        $status = Db::name('category')->where('id',$id)->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('category')->where('id',$id)->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('category')->where('id',$id)->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }

    }
    //添加分类
    public function categoryAdd()
    {
        if(request()->isAjax()){
            $param = input('post.');

            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }
            $res = Db::name('category')->insert($param);
            if($res){
                return json(['code' => 1, 'url' => 'category/index', 'msg' => '添加分类成功']);
            }else{
                return json(['code' => -1, 'url' => 'category/index', 'msg' => '添加分类失败，请重试']);
            }
        }
        //树形分类
        $list = Db::name('category')->select();
        $list = Data::tree($list,'name','id','pid');
        $this->assign('list',$list);
        return $this->fetch();
    }
    //编辑分类
    public function categoryEdit()
    {
        $id = input('param.id');
        if(request()->isAjax()){
            $param = input('post.');
            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }
            $data = Db::name("category")->where(array('id'=>$id))->update($param);
            if(false === $data){
                return json(['code' => -1, 'url' => 'category/index', 'msg' => '修改分类失败']);
            }else{
                return json(['code' => 1, 'url' => 'category/index', 'msg' => '修改分类成功']);
            }
        }
        $cate = Db::name('category')->select();
        $cate = Data::tree($cate,'name','id','pid');
        $list = Db::name('category')->where(array('id'=>$id))->find();
        $this->assign('list',$list);
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    //删除分类
    public function categoryDel()
    {
        $id = input('id');
        $res = Db::name('category')->where(array('id'=>$id))->delete();
        if($res){
            return json(['code' => 1, 'url' => 'category/index', 'msg' => '删除分类成功']);
        }else{
            return json(['code' => -1, 'url' => 'category/index', 'msg' => '删除分类，请重试']);
        }
    }
}